Pictures: Sorry, there ain't no real good pictures. There are
no diagrams either - just one screen shot, and one table. A picture
called LISA.IFF is included to demonstrate how the table should
appear.
Lisa --
Important
----------
This month's article requires that some programs appear on the coverdisk. The programs that must be included are:
miniFORTH
program1
program2
program3
program4
program5
program6
program7
Combined, they take up very little space. I've assumed they live on the disk in a drawer called 'miniforth'. Please see that whoever puts the disk together gets these files. Thank you.
The Final Robot Article
------------------------
No more hardware - this month John Kennedy describes the specially written programming language which will bring your creations to life.
Introduction
Building a robot is all very well, but how do you get it to move? So far, the only way we have been able to communicate with the input/output port has been through some Comms software: as the port is connected to the serial port, it is relatively easy to type in commands directly.
Unfortunately, controlling the hardware in this way is far from perfect. It's impossible to get the robot to make decisions itself, or to react to inputs from touch or light sensors - in fact, it's about as much of a robot as a remote control car.
To provide some semblance at Artificial Intelligence, we need a decent programming language. It would be entirely possible to use the ARexx scripting language which comes with all Amigas, but I though I would take this opportunity to show you a language I've always had a soft spot for -
FORTH.
FORTH was written in about 1970 by a fellow called Charles Moore. Chuck needed a language that was quick to develop in, used little memory and would control his Radio Telescope. He wanted a fourth generation language, but the operating system he was using only allowed five letter names - and
thus FORTH was born.
Back in the 1980's a little plastic computer called the Jupiter Ace appeared and totally failed to popularise the language. Being compact, FORTH was the ideal choice for a severely memory limited computer, and compared the most popular language in those days (Sinclair BASIC), it was ludicrously fast.
Recently Real 3D version 2 has been enhanced to feature a built-in FORTH system, so it seems we'll never be able to shake off one of the simplest yet most powerful languages ever designed.
FORTH PRINCIPLES
A stack is an area of memory which is used to temporarily store information, and FORTH makes heavy use of one. Even simple calculations require numbers so placed on a stack, so it's important that its operation is clearly understood. For starters, every number that appears in a FORTH program is immediately stacked, and even entering '20' at the keyboard will place a number on the stack.
This makes passing data to functions quite simple. For example, the FORTH word for printing a number is a full-stop, so if you were to enter the line:
20 .
you would see the number displayed on screen. The dot actually removes a number from the stack and performs an operation with it. Likewise, the FORTH word for addition, +, takes two numbers off the stack, adds them together and then stacks the sum. So, the line:
1 2 + .
will print 3 on the screen.
The FORTH system which came on the coverdisk contains about 80 words, and the more common ones are shown in the table. You can get a list of them by using the FORTH word VLIST, which stands for 'vocabulary list'.
The words supplied offer a range of arithmetic and logic operations, decision and loop control, external hardware control, simple graphics and mouse and joystick support.
USING MINIFORTH
The best way to learn how to use a programming language is to use it, so here's how to get miniFORTH working.
First of all, boot your Amiga from your normal Workbench disk. When all the clunking and whirring eventually stops, open a SHELL window. Now pop out your Workbench disk and insert the coverdisk.
We need to make the current directory the directory on the disk which contains the FORTH program and its example files, so enter the following at the prompt:
cd df0:miniFORTH (and return)
Now this assumes that the coverdisk has been produced in a particular way, and for various reasons that mightn't be the case. See the coverdisk pages for confirmation.
When you have got the current directory set, start the FORTH system by typing:
miniforth (and return again)
You should be greeted by a small welcome message, and 'OK'. The 'OK' is FORTH's way of telling you that everything is hunky-dory. It will say 'OK' every time it recognises a word. For now, enter:
VLIST (and return yet again)
You should see a large collection of meaningless (for the moment) text pop up on the screen. And, of course, the word 'OK'. Now for some arithmetic - try entering:
1 2 + .
This should display 3. Similarly, 10 5 * . should display 50. For some tricky subtraction, you will need to understand the way in which the stack works, or you'll get your sums all wrong.
The FORTH stack is a 'first in - last out' stack. That means if I stacked the numbers 1,2 and 3 in that order, when I popped them off I would get 3,2 and 1. So in order to subtract 1 from 100, you need to enter:
100 1 - .
And to divide 100 by 10, you enter:
100 10 / .
Note that miniFORTH is an integer based language, so floating point fractions aren't allowed at present. The words 1+, 1-, 2+ and 2- are short cut words for the commonly met situation where a number needs to be quickly changed.
100 2+ .
will display 102, and is exactly the same (but microscopically faster) than
100 2 + .
which makes use of '+'.
DIY WORDS
A FORTH program is a list of words, with each word making use of words previously defined. Although miniFORTH comes with about 80 words of its own, you'll probably want to write some new ones. Creating words is easy, and makes use of a colon (':') and a semi-colon (';'). For example, here's a FORTH definition of a word which does some arithmetic.
: SUMS
1 1 + . CR
1 2 + . CR
1 3 + . CR
;
The FORTH word CR tells the computer to take a new-line: it stands for carriage return. The word SUMS has already been entered for you, and is stored on the coverdisk in the same directory as the program. To load it, type
load program1
If all goes well, the familiar 'OK' should appear. Now do a vocabulary list (enter VLIST) and you should notice a new word right at the end. To find out more about it, enter VLISTFULL which will list its contents. If should look like our definition above, but with some more comments in round brackets. To execute this word simply type its name, so enter SUMS at the keyboard. As if by magic, the answers should appear.
If you want to look at the program as I entered it, it is a plain ASCII text file, so you can either display it with TYPE from the CLI, or by loading it into any word processor. You'll notice a lot of comments between round brackets - these are ignored by the FORTH system, but they are there for your benefit. Check all the other example programs for helpful information on what is going on!
The FORTH word LOAD takes a text file stored on disk, and treats it as though it were entered at the keyboard. To create your own words you will need to use a text editor to create a file, and then use miniFORTH to LOAD it in.
Check through the other example programs. Program3 is full of details on how to make use of variables and constants in miniFORTH (fun!) and Program4 demonstrates the graphical features of miniFORTH (rather slowly, I'm afraid). It also shows how to make a program autostart when your load it (by the way, type END to leave miniFORTH).
Program5 is a primitive paint program (click the mouse button to stop it). Program6 is the robot control program, but before you can use it there are still a few details that need dealt with.
ROBOTS ARE GO
If you remember, the input/output port expects serial data at a baud rate of 1200 baud maximum. In order to communicate with it, the Amiga must be talking at the right speed. To set the speed, find the Prefs drawer on your Workbench, and find the serial tool inside it. When you run this program,
you can select the speed that you wish the serial port to run at, so make sure it's set at 1200. After you hit save you can be sure that when you run miniFORTH it will talk to the robot at the right speed.
There are six words which communicate with the input/output port, and they correspond exactly the six control words which are detailed in the input/output port's instructions. For example CONFIGUREBYTE will take a number off the stack, and use it to set the Ins and Outs of the port. See the words defined in Program6 for more details. Note that if there isn't a working device attached to the serial port when the robot commands are given, the system will hang. This is because it is expecting the acknowledge signals from the port, so don't run it unless you do have the robot hardware present.
FURTHER IDEAS
Now that we have a full language to play with, our robot can start to think for itself. The example control program will only drive the buggy around in a square, but with input sensors you should be able to construct quite a complicated system, capable of finding its way around mazes and obstacles.
THE END
So that's the end of our Robot project. I hope you've found it useful, for not only have we looked at ways of interfacing just about anything to your Amiga, but we have also created a programming language especially to do it with.
Next month
Next month's DIY will be a retrospective of past projects, as I take a look back at some of the most common pitfalls (mostly all mine) which have befallen budding DIYers. Don't miss it!
{ Lisa - better check this with DAN, he might be expecting
the PARNET project. If he is, this is no problem, as long as
I am told! }
WORD DEFINITIONS
{Lisa, in this table the entries are separated by a row of
dashes. The items on each line are separated by a two
slashes (//). I know the tables usually get mucked up,
so I hope this helps. I know it won't, so I've included a
screen shot called LISA.IFF in this archive. This screen
shot is not for the magazine, rather just for you to look at
to get the table right. Good luck.}
Here is a list of some of the words supplied with the
miniFORTH system. For more details please check out the
example programs. If you are keen to use this language, I
recommend you get hold of a good library book on the